﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Models;
using IDAL;

namespace DAL
{
    

    public class CountInfoDAL:ICountInfo
    {
        public DataBase DB = new DataBase();
        public const string SQL_COUNT_INFO = @"select 1 as ItemID,'新闻分类' as ItemName,count(*) as ItemCount, 'admin_newscategory.aspx' as ItemURL from NewsCategoryInfo
                                            union all
                                            select 2,'新闻' , count(*),'admin_newslist.aspx' from NewsInfo
                                            union all
                                            select 3,'案例分类',count(*),'admin_workscategory.aspx' from WorksCategoryInfo
                                            union all
                                            select 4,'案例',count(*),'admin_works.aspx' from WorksInfo
                                            union all
                                            select 5,'客户',count(*),'admin_service.aspx?cid=2' from ServiceInfo where CategoryID = 2
                                            union all
                                            select 6,'合作伙伴',count(*),'admin_service.aspx?cid=1' from ServiceInfo where CategoryID = 1
                                            union all
                                            select 7,'招聘信息',count(*),'admin_career.aspx' from CareerInfo";


        public List<CountInfo> GetCountInfoList()
        {
            List<CountInfo> list = new List<CountInfo>();
            DB.DatabaseConnection();
            SqlDataReader reader = DB.GetdataRecords(SQL_COUNT_INFO);
            while (reader.Read())
            {
                CountInfo countinfo = new CountInfo();
                ReadtoCountInfo(reader, countinfo);
                list.Add(countinfo);
            }
            DB.CloseDB();
            return list;
        }

        public void ReadtoCountInfo(SqlDataReader reader, CountInfo countinfo)
        {
            try
            {
                countinfo.ItemID = Int32.Parse(reader["ItemID"].ToString());
            }
            catch
            { }
            countinfo.ItemName = reader["ItemName"] is DBNull ? "" : reader["ItemName"].ToString();
            try
            {
                countinfo.ItemCount = Int32.Parse(reader["ItemCount"].ToString());
            }
            catch
            { }
            countinfo.ItemURL = reader["ItemURL"] is DBNull?"":reader["ItemURL"].ToString();
        }
    }
}
